//
//  WKWebView+WKWebView_GestureRecognizer.m
//  CYWFrame
//
//  Created by Mac on 2017/12/14.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "WKWebView+WKWebView_GestureRecognizer.h"
#import "JTSImageViewController.h"
#import "LZPictureBrowser.h"

@implementation WKWebView (WKWebView_GestureRecognizer)

- (void)tapActionRecognizer:(UITapGestureRecognizer *)tap referenceView:(UIView *)view showFromViewController:(UIViewController *)vc imgArr:(NSArray *)imgArr {
    
    CGPoint pt = [tap locationInView:self];
    NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];
    
    
    [self evaluateJavaScript:imgURL completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        if (result) {
            
            NSLog(@"url = %@", result);
            NSLog(@"arr = %@", imgArr);
            
            if(![result hasPrefix:@"http"])return;
            int index = 0;
            for (int i = 0; i < imgArr.count; i++) {
                index = i;
                NSLog(@"--%d", index);
                if ([imgArr[i] isEqualToString:result]) {
                    break;
                }
            }
            
            LZPictureBrowser *mvc = [[LZPictureBrowser alloc] init];
            [mvc showWithPictureURLs:imgArr atIndex:index];
            
//            JTSImageInfo *imageInfo = [[JTSImageInfo alloc] init];
//            imageInfo.imageURL = [NSURL URLWithString:result];
//            imageInfo.referenceView = view;
//            
//            JTSImageViewController *imageViewer = [[JTSImageViewController alloc]
//                                                   initWithImageInfo:imageInfo
//                                                   mode:JTSImageViewControllerMode_Image
//                                                   backgroundStyle:JTSImageViewControllerBackgroundOption_Blurred];
//            [imageViewer showFromViewController:vc transition:JTSImageViewControllerTransition_FromOffscreen];
//
        }
    }];
}

@end
